﻿using System;
using System.Collections;
using System.Reflection;
using System.Xml;
using YArchitech.Controls;

namespace YArchitech.Revit.MEP.Opening
{
	public class XmlConfigDataBase
	{
		public XmlConfigDataBase(string strXmlFilePath)
		{
			this.m_strXmlFilePath = strXmlFilePath;
			this.ReadCfgDataFromXml();
		}

		protected virtual bool ReadCfgDataFromXml()
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(this.m_strXmlFilePath);
				foreach (object obj in xmlDocument.GetElementsByTagName(base.GetType().Name))
				{
					IEnumerator enumerator2 = ((XmlNode)obj).ChildNodes.GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj2 = enumerator2.Current;
							XmlAttributeCollection attributes = ((XmlNode)obj2).Attributes;
							if (attributes.Count != 0)
							{
								XmlAttribute xmlAttribute = attributes[0];
								foreach (PropertyInfo propertyInfo in base.GetType().GetProperties())
								{
									if (!(propertyInfo.Name != xmlAttribute.Name))
									{
										string a = propertyInfo.PropertyType.Name.ToLower();
										object value;
										if (!(a == "string"))
										{
											if (!(a == "double"))
											{
												if (!(a == "boolean"))
												{
													value = ConfigDataOperate.ConvertString2Int32(xmlAttribute.Value);
												}
												else
												{
													value = (xmlAttribute.Value == "True");
												}
											}
											else
											{
												value = ConfigDataOperate.ConvertString2Double(xmlAttribute.Value);
											}
										}
										else
										{
											value = xmlAttribute.Value;
										}
										propertyInfo.SetValue(this, value, null);
										break;
									}
								}
							}
						}
						break;
					}
				}
			}
			catch
			{
				this.InitDataByDefaultConfig();
			}
			return true;
		}

		protected virtual void InitDataByDefaultConfig()
		{
		}

		public virtual bool WriteCfgData2Xml()
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
				XmlNode xmlNode = xmlDocument.CreateElement(base.GetType().Name);
				xmlDocument.AppendChild(xmlNode);
				foreach (PropertyInfo propertyInfo in base.GetType().GetProperties())
				{
					string a = propertyInfo.PropertyType.Name.ToLower();
					string value;
					if (!(a == "string"))
					{
						if (!(a == "double"))
						{
							if (!(a == "boolean"))
							{
								value = ((int)propertyInfo.GetValue(this, null)).ToString();
							}
							else
							{
								value = ((bool)propertyInfo.GetValue(this, null)).ToString();
							}
						}
						else
						{
							value = ((double)propertyInfo.GetValue(this, null)).ToString("F0");
						}
					}
					else
					{
						value = (string)propertyInfo.GetValue(this, null);
					}
					XmlElement xmlElement = xmlDocument.CreateElement("ConfigDataItem");
					xmlElement.SetAttribute(propertyInfo.Name, value);
					xmlNode.AppendChild(xmlElement);
				}
				xmlDocument.Save(this.m_strXmlFilePath);
			}
			catch
			{
				YJKMessageBox.Show("保存用户数据发生错误!");
			}
			return false;
		}

		private string m_strXmlFilePath = "";
	}
}
